<template>
  <div class="es_report">
    <div class="report-container">
      <div class="report-item">
        <p class="title">年度电费 <span class="price">¥{{anniversarFee}}</span></p>
        <p class="sub-title">*本页数据由MecKey云计算提供</p>
        <div id="esfee"></div>
      </div>
      <div :class="{blur: cover}">
        <div class="report-item">
          <p class="title m-b-10">每月平均电费 <span class="price">¥{{averageFee}}</span></p>
          <div class="fl">
            <div class="width-50">
              <p class="fl fl-align-center m-b-5">
                <img src="@/assets/fee.svg" class="icon m-r-5" alt="fee-icon">最高电费
              </p>
              <p>{{maxMonth}}月 <span class="price">¥{{maxFee}}</span></p>
            </div>
            <div class="width-50">
              <p class="fl fl-align-center m-b-5">
                <img src="@/assets/fee.svg" class="icon m-r-5" alt="fee-icon">最低电费
              </p>
              <p>{{minMonth}}月 <span class="price">¥{{minFee}}</span></p>
            </div>
          </div>
        </div>
        <div class="report-item">
          <p class="title">年度一共使用高峰电 <span class="price">{{highFees}}</span>度</p>
          <p class="sub-title">*高峰电费比低谷电费高出2～3倍</p>
          <div id="highLowFee"></div>
        </div>
        <div class="report-item">
          <p class="title">每年收益<span class="price">¥{{saveMoneyPerYear}}</span> 20年收益<span class="price">¥{{saveMoney}}</span></p>
          <p class="sub-title">*光伏板出厂寿命为20年，每年会有3%左右的效率衰退</p>
        </div>
      </div>
      <div class="cover fl fl-align-top fl-space-center" v-if="cover">
        <a href="javascript:;" class="open" @click="handleCover">查看全部</a>
      </div>
    </div>
    <div class="mk-alert" v-show="alert" :class="{active: alert}">
      <div class="cover"></div>
      <div class="form-container">
        <div class="form-wrapper fl fl-space-bw">
          <p class="title">验证后查看全部信息</p>
          <a class="close" @click="handleClose">关闭</a>
        </div>
        <div class="form-wrapper">
          <input type="number" class="text" placeholder="请输入手机号码" v-model="phone">
        </div>
        <div class="form-wrapper fl">
          <input type="text" class="text" placeholder="验证码" v-model="code">
          <a class="assist-btn" @click="handleCode">{{codeMsg}}</a>
        </div>
        <div class="form-wrapper">
          <p class="sub-title">
            *手机号用于验证身份，我司将严格保护您的隐私不泄漏给第三方机构。
          </p>
        </div>
        <div class="form-wrapper">
          <a class="submit" @click="handleLogin">确认</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import util from '@/utils/echartsOption'
import tools from '../utils/common'
export default {
  name: 'ESReport',
  data () {
    return {
      alert: false,
      cover: true,
      waitCode: false,
      codeMsg: '获取验证码',
      codeTimeOut: null,
      phone: null,
      code: null,
      displayChart: false,
      esFeeOpt: {},
      esFeeChart: {},
      HLOpt: {},
      HLChart: {}
    }
  },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    anniversarFee: { type: Number },
    perMonthFee: { type: Array },
    averageFee: { type: Number },
    maxFee: { type: Number },
    minFee: { type: Number },
    maxMonth: { type: Number },
    minMonth: { type: Number },
    highFees: { type: Number },
    flatFees: { type: Number },
    lowFees: { type: Number },
    unitFee: { type: Number },
    esShow: { type: Number }
  },
  watch: {
    show () {
      if (this.show && !this.displayChart) {
        console.log(this.esShow)
        if (this.esShow === 1) {
          this.cover = false
        }
        this.displayChart = true
        const echarts = this.$echarts
        const mons = util.numbers(12)
        const feeData = this.perMonthFee
        this.esFeeOpt = util.SimpleLineChart(mons, feeData)
        util.LinearColor(this.esFeeOpt, echarts)
        this.esFeeChart = echarts.init(document.getElementById('esfee'))
        this.esFeeChart.setOption(this.esFeeOpt)

        this.HLOpt = util.BriefPie(['高峰', '平段', '低谷'], [this.highFees, this.flatFees, this.lowFees], ['rgb(192,54,54)', '#ffa70b', '#187a2c'])
        this.HLChart = echarts.init(document.getElementById('highLowFee'))
        this.HLChart.setOption(this.HLOpt)
      }
    }
  },
  computed: {
    saveMoney () {
      let number = parseInt(this.unitFee * this.highFees)
      let total = 0
      for (let i = 0; i < 20; i++) {
        total += number
        number = number * 0.95
      }
      return parseInt(total)
    },
    saveMoneyPerYear () {
      return parseInt(this.unitFee * this.highFees)
    }
  },
  methods: {
    handleCover () {
      this.alert = true
    },
    handleClose () {
      this.alert = false
    },
    handleCode () {
      if (!tools.validatePhone(this.phone)) {
        alert('手机号码格式错误')
        return false
      }
      const _this = this
      if (!this.waitCode) {
        _this.getCode()
        this.codeMsg = 60 + 's'
        this.waitCode = true
        this.codeTimeOut = setInterval(function () {
          let time = parseInt(_this.codeMsg)
          if (time === 0) {
            _this.codeMsg = '获取验证码'
            clearInterval(_this.codeTimeOut)
            _this.codeTimeOut = null
            _this.waitCode = false
          } else {
            _this.codeMsg = --time + 's'
          }
        }, 1000)
      }
    },
    getCode () {
      this.$http.post('/send_message', {
        mobile: this.phone
      })
    },
    handleLogin () {
      this.$http.post('/validate_send_message', {
        mobile: this.phone,
        code: this.code
      }).then(res => {
        if (res.data.status === 1) {
          this.cover = false
          this.alert = false
          this.$http.post('/set_es_user_data', {
            mobile: this.phone
          })
        } else {
          alert('验证码错误')
        }
      })
    }
  }
}
</script>

<style scoped>
  .mk-alert {
    position: fixed;
    z-index: 99999;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    padding: 10px 24px;
    display: flex;
    align-items: center;
  }
  .mk-alert .cover {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #000000;
    opacity: 0;
    z-index: 1;
    transition: opacity .3s;
  }
  .mk-alert .form-container {
    position: relative;
    z-index: 9;
    width: 100%;
    background-color: #ffffff;
    padding: 16px;
    border-radius: 6px;
    transform: scale(0.9);
    transition: all .3s;
    animation: fadeOutIn .3s;
  }
  .mk-alert.active .cover{
    opacity: .6;
  }
  .mk-alert.active .form-container {
    transform: scale(1);
  }
  .mk-alert .form-wrapper .submit {
    display: inline-block;
    margin: 0;
    width: 100%;
    border-radius: 6px;
    padding: 0 20px;
    line-height: 40px;
  }
  .mk-alert .sub-title {
    font-size: 12px;
    color: #9a9a9a;
  }
  .es_report {
    background-color: #0095ff !important;
  }
  .open {
    width: 90%;
    text-align: center;
    text-decoration: none;
    background-color: #0095ff;
    color: #ffffff;
    font-weight: bold;
    padding: 10px 20px;
    border-radius: 6px;
  }
  .report-container {
    background-color: #ffffff;
    border-radius: 6px;
    overflow: hidden;
    position: relative;
  }
  .report-item {
    padding: 36px 10px;
    border-bottom: 1px dashed #0095ff;
    background-color: #ffffff;
    position: relative;
  }
  .report-item:before, .report-item:after {
    content: ' ';
    display: block;
    width: 16px;
    height: 16px;
    border-radius: 20px;
    position: absolute;
    bottom: -8px;
    left: -8px;
    background-color: #0095ff;
    z-index: 9;
  }
  .report-item:after {
    left: auto;
    right: -8px;
  }
  .report-item:last-child {
    border-bottom: none;
  }
  .report-item:last-child:before, .report-item:last-child:after {
    display: none;
  }
  #esfee {
    width: 100%;
    height: 140px;
  }
  #highLowFee {
    width: 100%;
    height: 220px;
  }
  .report-item .title {
    font-size: 18px;
    font-weight: bold;
  }
  .report-item .sub-title {
    font-size: 13px;
    color: #9a9a9a;
  }
  .icon {
    width: 20px;
    height: 20px;
  }
  .price {
    color: #0095ff;
    font-weight: bold;
  }
  .blur {
    filter: blur(5px);
  }
  .cover {
    width: 100%;
    height: 580px;
    position: absolute;
    left: 0;
    bottom: 0;
    z-index: 9;
    overflow: hidden;
    padding-bottom: 20px;
  }
  .cover:after {
    content: ' ';
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-image: linear-gradient(rgba(255, 255, 255, .3), rgba(205, 205, 205, 0.6));
    z-index: -1;
    margin: -30px;
  }
  .form-wrapper.fl input.text {
    flex: 1;
    margin-right: 10px;
  }
</style>
